package gui.history;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ResourceBundle;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;

import gui.Element;
import history.DataHistory;
import history.DataHistoryListener;

public class DataHistoryElement implements Element, DataHistoryListener, ActionListener {
	
	public DataHistoryElement() {
	}
	
	public DataHistoryElement(DataHistory historyProvider) {
		this.historyProvider = historyProvider;
		historyProvider.registerListener(this);
		
	}
	
	void registerDataHistoryProvider(DataHistory historyProvider) {
		this.historyProvider = historyProvider;
		historyProvider.registerListener(this);
		if(table != null) {
			tableModel = new DataHistoryTableModel(historyProvider);
			table.setModel(tableModel);
			tableModel.fireTableDataChanged();  
		}
	}
	
	@Override
	public void init() {
		if (historyProvider == null)
			throw new NullPointerException();
		msg = ResourceBundle.getBundle("DataHistoryGUI");
		masterPanel = new JPanel();
		tableModel = new DataHistoryTableModel(historyProvider);
		table = new JTable(tableModel);
		table.getColumn(table.getColumnName(1)).setCellRenderer(new DoubleRenderer());
		table.getColumn(table.getColumnName(0)).setCellRenderer(new DateRenderer());
		/* WTF those are ignored ?:> */
		table.getColumn(table.getColumnName(1)).setPreferredWidth(30);
		table.getColumn(table.getColumnName(0)).setPreferredWidth(50);
		scroll = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		saveButton = new JButton(msg.getString("SaveButtonMsg"));
		saveButton.addActionListener(this);
		masterPanel.setLayout(new BoxLayout(masterPanel, BoxLayout.Y_AXIS));
		masterPanel.add(scroll);
		masterPanel.add(saveButton);
		fileChooser = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter(msg.getString("FileExtensionDescription"), msg.getString("FileExtension"));
		fileChooser.setFileFilter(filter);
	}

	@Override
	public JComponent getComponent() {
		return masterPanel;
	}

	@Override
	public String getCaption() {
		return msg.getString("PanelName");
	}

	@Override
	public boolean isLeftPanel() {
		/* due to lack of documentation - how the fuck would I know? */
		return true;
	}

	protected JScrollPane scroll;
	protected JTable table;
	protected DataHistory historyProvider;
	protected DataHistoryTableModel tableModel;
	protected JPanel masterPanel;
	protected JButton saveButton;
	protected JFileChooser fileChooser;
	private ResourceBundle msg;
	
	@Override
	public void notifyIncomingData() {
		if (tableModel!=null)
			tableModel.fireTableDataChanged();  
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		int retVal = fileChooser.showSaveDialog(masterPanel);
		if(retVal == JFileChooser.APPROVE_OPTION) {
			try {
				historyProvider.save(fileChooser.getSelectedFile());
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(masterPanel, msg.getString("IOError"), msg.getString("IOErrorWindowName"), JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
